package com.clannad.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.clannad.commonutils.JwtUtils;
import com.clannad.commonutils.MD5;
import com.clannad.commonutils.R;
import com.clannad.educenter.entity.UcenterMember;
import com.clannad.educenter.entity.vo.RegisterVo;
import com.clannad.educenter.mapper.UcenterMemberMapper;
import com.clannad.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clannad.servicebase.exceptionhandler.GuliException;
import com.sun.org.apache.bcel.internal.generic.LADD;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author clannad
 * @since 2022-08-17
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"手机号或密码为空");
        }
        LambdaQueryWrapper<UcenterMember> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UcenterMember::getMobile,mobile);
        UcenterMember mobileMember = this.getOne(lambdaQueryWrapper);
        if(mobileMember==null){
            throw new GuliException(20001,"登录失败，请检查手机号或密码");
        }
        if(!mobileMember.getPassword().equals(MD5.encrypt(password))){
            throw new GuliException(20001,"登录失败，请检查手机号或密码");
        }
        if(mobileMember.getIsDisabled()){
            throw new GuliException(20001,"登录失败，您已被禁用请联系客服或管理员");
        }
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(),mobileMember.getNickname());
        System.out.println(jwtToken);
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        UcenterMember ucenterMember=new UcenterMember();
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        if(StringUtils.isEmpty(code)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败，请检查必填项");
        }
        String codes= redisTemplate.opsForValue().get(mobile);
        if(StringUtils.isEmpty(codes)){
            throw new GuliException(20001,"注册失败，验证码已过期");
        }

        if(!codes.equals(code)){
            throw new GuliException(20001,"注册失败，验证码错误");
        }

        LambdaQueryWrapper<UcenterMember> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UcenterMember::getMobile,mobile);
        int count = this.count(lambdaQueryWrapper);
        if(count>0){
            throw new GuliException(20001,"手机号已被注册");
        }

        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDeleted(false);
        ucenterMember.setAvatar("https://edu-guli-1010clannad.oss-cn-nanjing.aliyuncs.com/2022/08/17/6a38ae76da2f4133b00ec27bd66dd2ef123456.jpg");
        this.save(ucenterMember);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        LambdaQueryWrapper<UcenterMember> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UcenterMember::getOpenid,openid);
        UcenterMember one = this.getOne(lambdaQueryWrapper);
        return one;
    }

    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
